package com.game.base.server.file.bmoblibrary;

import android.text.TextUtils;

import com.game.base.dir.DirType;
import com.game.base.server.file.bmoblibrary.listener.UploadListener;
import com.game.base.utils.bitmap.BitmapUtil;
import com.game.base.utils.file.FileUtil;
import com.game.base.utils.log.Logger;
import com.game.base.utils.toast.ToastUtil;

import java.io.File;
import java.util.List;

import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UploadBatchListener;
import cn.bmob.v3.listener.UploadFileListener;

/**
 * 文件上传操作类 {@link 'http://doc.bmob.cn/data/android/example/#_10'}
 * <p>
 * 暂时无法使用，需要绑定个人备案域名{https://www.bmob.cn/site/news/98}
 * Created by Su on 2019/5/8.
 */
public class FileUploadManager {

    /**
     * 图片:  单个文件上传
     */
    public static void uploadBlockPic(String path, UploadListener listener) {
        if (!FileUtil.isExist(path)) {
            ToastUtil.showShort("文件路径无效");
            return;
        }
        String smallPath = BitmapUtil.getSmallBitmapAndSave(path, DirType.getUploadDir());
        String uploadPath = FileUtil.isExist(smallPath) ? smallPath : path;

        File file = new File(uploadPath);
        final BmobFile bmobFile = new BmobFile(file);
        bmobFile.uploadblock(new UploadFileListener() {
            @Override
            public void done(BmobException e) {
                if (TextUtils.equals(uploadPath, smallPath)) {
                    FileUtil.deleteFile(uploadPath);
                }
                if (listener != null) {
                    listener.done(bmobFile, e);
                }
            }

            @Override
            public void onProgress(Integer value) {
                super.onProgress(value);
                if (listener != null) {
                    listener.onProgress(value);
                }
            }
        });
    }

    /**
     * 批量上传
     */
    public void uploadBatch(String... paths) {
        final String[] filePaths = new String[paths.length];
        for (int i = 0; i < paths.length; i++) {
            filePaths[i] = paths[i];
        }
        // 依次上传文件夹里面的文件
        BmobFile.uploadBatch(filePaths, new UploadBatchListener() {

            /**
             * onSuccess方法就会执行次数 = 上传文件个数
             *
             * @param files 上传完成后的BmobFile集合, 方便将BmobFile存入表中
             * @param urls  上传文件的完整url地址
             */
            @Override
            public void onSuccess(List<BmobFile> files, List<String> urls) {
                Logger.d("uploadBatch -onSuccess: " + urls.size() + "-----" + files + "----" + urls);
                if (urls.size() == filePaths.length) {//如果数量相等，则代表文件全部上传完成
                    //do something
                }
            }

            @Override
            public void onError(int statuscode, String errormsg) {
                Logger.d("uploadBatch 错误码" + statuscode + ",错误描述：" + errormsg);
            }

            /**
             * @param curIndex      表示当前第几个文件正在上传
             * @param curPercent    表示当前上传文件的进度值（百分比）
             * @param total         表示总的上传文件数
             * @param totalPercent  表示总的上传进度（百分比）
             */
            @Override
            public void onProgress(int curIndex, int curPercent, int total,
                                   int totalPercent) {
            }
        });
    }
}
